{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f92e330b",
   "metadata": {},
   "source": [
    "# 1、百度Access token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3a6252ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"error\":\"unsupported_grant_type\",\"error_description\":\"The authorization grant type is not supported\"}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import requests\n",
    "import json\n",
    "\n",
    "\n",
    "def main():\n",
    "        \n",
    "    url = \"https://aip.baidubce.com/oauth/2.0/token?client_id=kGTTjbH8qoiHxeVhXZwptbDs=v3CGPr9BkHQhRISTyhBlnMZ7Pr4HI5Cd\"\n",
    "    \n",
    "    payload = \"\"\n",
    "    headers = {\n",
    "        'Content-Type': 'application/json',\n",
    "        'Accept': 'application/json'\n",
    "    }\n",
    "    \n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    \n",
    "    print(response.text)\n",
    "    \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "92c81d89",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24.334a6c522ecb048b28c833945a978414.2592000.1681646044.282335-31381620\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import requests\n",
    "import json\n",
    "\n",
    "\n",
    "def main():\n",
    "        \n",
    "    url = \"https://aip.baidubce.com/oauth/2.0/token?client_id=kGTTjbH8qoiHxeVhXZwptbDs&client_secret=v3CGPr9BkHQhRISTyhBlnMZ7Pr4HI5Cd&grant_type=client_credentials\"\n",
    "    \n",
    "    payload = \"\"\n",
    "    headers = {\n",
    "        'Content-Type': 'application/json',\n",
    "        'Accept': 'application/json'\n",
    "    }\n",
    "    \n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    \n",
    "    print(response.json()[\"access_token\"])\n",
    "    \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cc31a17",
   "metadata": {},
   "source": [
    "# 2.通用物体识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "eb7ed0b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'keyword': '美女', 'score': 0.747556, 'root': '人物-人物特写', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E7%BE%8E%E5%A5%B3/109596', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/91529822720e0cf3acdfc0280046f21fbe09aa3d', 'description': '美女，汉语词语，拼音是měi nǚ，意思是容貌姣好、仪态优雅的女子。'}}, {'keyword': '人物特写', 'score': 0.586271, 'root': '人物-人物特写'}, {'keyword': '时装T台', 'score': 0.428928, 'root': '人物-人物特写'}, {'keyword': '清纯少女', 'score': 0.236239, 'root': '人物-人物特写'}, {'keyword': '书籍', 'score': 0.069743, 'root': '非自然图像-书籍封面'}], 'log_id': 1636705909940390119}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('222.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img,'baike_num':1}\n",
    "access_token = '24.334a6c522ecb048b28c833945a978414.2592000.1681646044.282335-31381620'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4aa1c194",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
